home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / INTERN.C < prev    next >
C/C++ Source or Header  |  1992-01-14  |  6KB  |  200 lines

  1. /* -*-C-*-
  2.  
  3. $Header: /scheme/src/microcode/RCS/intern.c,v 9.52 1992/01/15 02:31:10 jinx Exp $
  4.  
  5. Copyright (c) 1987-92 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. /* String hash functions and interning of symbols. */
  36.  
  37. #include "scheme.h"
  38. #include "prims.h"
  39. #include "trap.h"
  40.  
  41. /* Hashing strings */
  42.  
  43. #define STRING_HASH_BITS 16
  44.  
  45. static unsigned int
  46. DEFUN (string_hash, (string), SCHEME_OBJECT string)
  47. {
  48.   fast unsigned char * scan = (STRING_LOC (string, 0));
  49.   fast unsigned char * end = (scan + (STRING_LENGTH (string)));
  50.   fast unsigned int result = 0;
  51.   while (scan < end)
  52.     {
  53.       result <<= 1;
  54.       result |= (result >> STRING_HASH_BITS);
  55.       result ^= (*scan++);
  56.       result &= ((1 << STRING_HASH_BITS) - 1);
  57.     }
  58.   return (result);
  59. }
  60.  
  61. static Boolean
  62. DEFUN (string_equal, (string1, string2),
  63.        SCHEME_OBJECT string1
  64.        AND SCHEME_OBJECT string2)
  65. {
  66.   fast unsigned char * scan1 = (STRING_LOC (string1, 0));
  67.   fast unsigned char * scan2 = (STRING_LOC (string2, 0));
  68.   fast long length = (STRING_LENGTH (string1));
  69.   fast unsigned char * end1 = (scan1 + length);
  70.   if (scan1 == scan2)
  71.     return (true);
  72.   if (length != (STRING_LENGTH (string2)))
  73.     return (false);
  74.   while (scan1 < end1)
  75.     if ((*scan1++) != (*scan2++))
  76.       return (false);
  77.   return (true);
  78. }
  79.  
  80. static SCHEME_OBJECT *
  81. DEFUN (find_symbol_internal, (string), SCHEME_OBJECT string)
  82. {
  83.   fast SCHEME_OBJECT * bucket;
  84.   {
  85.     fast SCHEME_OBJECT obarray = (Get_Fixed_Obj_Slot (OBArray));
  86.     bucket =
  87.       (MEMORY_LOC (obarray,
  88.            (((string_hash (string)) % (VECTOR_LENGTH (obarray)))
  89.             + 1)));
  90.   }
  91.   while ((*bucket) != EMPTY_LIST)
  92.     {
  93.       fast SCHEME_OBJECT symbol = (PAIR_CAR (*bucket));
  94.       if (string_equal (string, (FAST_MEMORY_REF (symbol, SYMBOL_NAME))))
  95.     return (PAIR_CAR_LOC (*bucket));
  96.       bucket = (PAIR_CDR_LOC (*bucket));
  97.     }
  98.   return (bucket);
  99. }
  100.  
  101. /* Set this to be informed of symbols as they are interned. */
  102. void (*intern_symbol_hook) () = ((void (*) ()) 0);
  103.  
  104. static SCHEME_OBJECT
  105. DEFUN (link_new_symbol, (symbol, cell),
  106.        SCHEME_OBJECT symbol
  107.        AND SCHEME_OBJECT * cell)
  108. {
  109.   /* `symbol' does not exist yet in obarray.  `cell' points to the
  110.      cell containing the final '() in the list.  Replace this
  111.      with a cons of the new symbol and '() (i.e. extend the
  112.      list in the bucket by 1 new element). */
  113.  
  114.   fast SCHEME_OBJECT result = (OBJECT_NEW_TYPE (TC_INTERNED_SYMBOL, symbol));
  115.   (*cell) = (cons (result, EMPTY_LIST));
  116.   if (intern_symbol_hook != ((void (*) ()) 0))
  117.     (*intern_symbol_hook) (result);
  118.   return (result);
  119. }
  120.  
  121. SCHEME_OBJECT
  122. DEFUN (find_symbol, (string), SCHEME_OBJECT string)
  123. {
  124.   fast SCHEME_OBJECT result = (* (find_symbol_internal (string)));
  125.   return ((result == EMPTY_LIST) ? SHARP_F : result);
  126. }
  127.  
  128. SCHEME_OBJECT
  129. DEFUN (string_to_symbol, (string), SCHEME_OBJECT string)
  130. {
  131.   fast SCHEME_OBJECT * cell = (find_symbol_internal (string));
  132.   if ((*cell) != EMPTY_LIST)
  133.     return (*cell);
  134.   Primitive_GC_If_Needed (2);
  135.   {
  136.     fast SCHEME_OBJECT symbol =
  137.       (MAKE_POINTER_OBJECT (TC_UNINTERNED_SYMBOL, Free));
  138.     (Free [SYMBOL_NAME]) = string;
  139.     (Free [SYMBOL_GLOBAL_VALUE]) = UNBOUND_OBJECT;
  140.     Free += 2;
  141.     return (link_new_symbol (symbol, cell));
  142.   }
  143. }
  144.  
  145. SCHEME_OBJECT
  146. DEFUN (intern_symbol, (symbol), SCHEME_OBJECT symbol)
  147. {
  148.   fast SCHEME_OBJECT * cell =
  149.     (find_symbol_internal (FAST_MEMORY_REF (symbol, SYMBOL_NAME)));
  150.   return
  151.     (((*cell) != EMPTY_LIST)
  152.      ? (*cell)
  153.      : (link_new_symbol (symbol, cell)));
  154. }
  155.  
  156. DEFINE_PRIMITIVE ("FIND-SYMBOL", Prim_find_symbol, 1, 1,
  157.   "(FIND-SYMBOL STRING)\n\
  158. Returns the symbol whose name is STRING, or #F if no such symbol exists.")
  159. {
  160.   PRIMITIVE_HEADER (1);
  161.  
  162.   CHECK_ARG (1, STRING_P);
  163.   PRIMITIVE_RETURN (find_symbol (ARG_REF (1)));
  164. }
  165.  
  166. DEFINE_PRIMITIVE ("STRING->SYMBOL", Prim_string_to_symbol, 1, 1,
  167.   "(STRING->SYMBOL STRING)\n\
  168. Returns the symbol whose name is STRING, constructing a new symbol if needed.")
  169. {
  170.   PRIMITIVE_HEADER (1);
  171.  
  172.   CHECK_ARG (1, STRING_P);
  173.   PRIMITIVE_RETURN (string_to_symbol (ARG_REF (1)));
  174. }
  175.  
  176. DEFINE_PRIMITIVE ("STRING-HASH", Prim_string_hash, 1, 1,
  177.   "(STRING-HASH STRING)\n\
  178. Return a hash value for a string.  This uses the hashing\n\
  179. algorithm used for interning symbols.  It is intended for use by\n\
  180. the reader in creating interned symbols.")
  181. {
  182.   PRIMITIVE_HEADER (1);
  183.  
  184.   CHECK_ARG (1, STRING_P);
  185.   PRIMITIVE_RETURN (LONG_TO_UNSIGNED_FIXNUM (string_hash (ARG_REF (1))));
  186. }
  187.  
  188. DEFINE_PRIMITIVE ("STRING-HASH-MOD", Prim_string_hash_mod, 2, 2,
  189.   "(STRING-HASH-MOD STRING DENOMINATOR)\n\
  190. DENOMINATOR must be a nonnegative integer.\n\
  191. Equivalent to (MOD (STRING-HASH STRING) DENOMINATOR).")
  192. {
  193.   PRIMITIVE_HEADER (2);
  194.  
  195.   CHECK_ARG (1, STRING_P);
  196.   PRIMITIVE_RETURN
  197.     (LONG_TO_UNSIGNED_FIXNUM
  198.      ((string_hash (ARG_REF (1))) % (arg_nonnegative_integer (2))));
  199. }
  200.